﻿using AutoMapper;
using FarmSmartSleep.Common.Domain.BodyWeight;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepWeightCommands
{
    public class BodyMeanCreateHandler : IRequestHandler<BodyMeanCreateCommand, Result>
    {
        private readonly IBaseRepository<BodyMeans> bodymeanbaseRepository;
        private readonly IMapper mapper;

        public BodyMeanCreateHandler(IBaseRepository<BodyMeans> bodymeanbaseRepository, IMapper mapper)
        {
            this.bodymeanbaseRepository = bodymeanbaseRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 体尺测定添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BodyMeanCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.add };
            BodyMeans bodyMeans = mapper.Map<BodyMeans>(request);
            var res = bodymeanbaseRepository.GetAll().Where(x => x.EarNumber == request.EarNumber).FirstOrDefault();
            //判断唯一
            if (res != null)
            {
                result.Code = -1;
            }
            else
            {
                var list = bodymeanbaseRepository.Add(bodyMeans);
                result.Code = list;
            }

            return result;
        }
    }
}
